package com.org.zp.ctrl.search;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.zp.R;
import com.org.zp.app.RecApp;

public class SearchJobActivity extends BaseActivity {

	private RelativeLayout p_position_name;// 职位名称
	private TextView p_name_txt;// 职位名称
	private RelativeLayout p_type_main;// 行业
	private TextView p_position_hangye;// 行业
	private RelativeLayout p_address_main;// 工作地点
	private TextView p_address_txt;// 工作地点
	private EditText keyword;// 关键字 p_keyword_title
	private RelativeLayout p_positiontype_main;// 职位性质
	private TextView p_positiontype_txt;// 职位性质
	private RelativeLayout p_companysize_main;// 公司规模
	private TextView p_companysize_txt;// 公司规模
	private RelativeLayout p_salary_main;// 月薪范围
	private TextView p_salary_txt;// 月薪范围
	private TextView title;
	private Button back_btn, top_right_btn;
	private Button search_button;
	private RecApp mApp;

	ProgressDialog progressDialog;

	private boolean mFullSearch = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.position_search);

		if (!(ActivityTaskManager.getInstance()
				.containsName("SearchJobActivity"))) {
			ActivityTaskManager.getInstance().putActivity("SearchJobActivity",
					this);
		}

		mFullSearch = getIntent().getBooleanExtra("full", false);

		mApp = getMyApplication();
		title = (TextView) findViewById(R.id.headTitle);
		back_btn = (Button) findViewById(R.id.back);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.e("TMG", "back on click");
				ActivityTaskManager.getInstance().removeActivity(
						"SearchJobActivity");
			}
		});

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setVisibility(View.GONE);
		search_button = (Button) findViewById(R.id.p_query_bt);
		p_position_name = (RelativeLayout) findViewById(R.id.p_position_name);//
		p_type_main = (RelativeLayout) findViewById(R.id.p_type_main);//
		p_address_main = (RelativeLayout) findViewById(R.id.p_address_main);//
		p_positiontype_main = (RelativeLayout) findViewById(R.id.p_positiontype_main);// 公司性质
		p_companysize_main = (RelativeLayout) findViewById(R.id.p_companysize_main);// 公司规模
		p_salary_main = (RelativeLayout) findViewById(R.id.p_salary_main);// 月薪范围
		p_name_txt = (TextView) findViewById(R.id.p_name_txt);
		p_position_hangye = (TextView) findViewById(R.id.p_type_txt);
		p_address_txt = (TextView) findViewById(R.id.p_address_txt);
		p_positiontype_txt = (TextView) findViewById(R.id.p_positiontype_txt);
		p_companysize_txt = (TextView) findViewById(R.id.p_companysize_txt);
		p_salary_txt = (TextView) findViewById(R.id.p_salary_txt);
		keyword = (EditText) findViewById(R.id.p_keyword_txt);// 关键字
		keyword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// mApp.keyword = keyword.getText().toString();
			}
		});
		title.setText("职位搜索");
		initCompents();
		// 搜索按钮
		search_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * if (null == p_address_txt.getText() ||
				 * "".equals(p_address_txt.getText())) { Toast toast =
				 * Toast.makeText(getApplicationContext(), "工作地点不能为空！",
				 * Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);
				 * toast.show(); return; }
				 */

				if (!(HttpUtils.checkNetwork(SearchJobActivity.this, true))) {
					return;
				}

				Intent intent = new Intent(SearchJobActivity.this,
						SearchJobResultActivity.class);
				intent.putExtra("key", keyword.getText().toString());
				startActivity(intent);
			}
		});
	}

	private void initCompents() {

		if (null != mApp.keyword && !"".equals(mApp.keyword)) {
			keyword.setText(mApp.keyword);
		}

		if (null != mApp.hPositionName.get("position_name_second")
				&& !mApp.hPositionName.get("position_name_second").equals(""))
			p_name_txt.setText(mApp.hPositionName.get("position_name_second"));
		p_name_txt.setTextColor(Color.BLACK);
		if (null != mApp.hPositionName.get("position_hangye")
				&& !mApp.hPositionName.get("position_hangye").equals(""))
			p_position_hangye
					.setText(mApp.hPositionName.get("position_hangye"));
		p_position_hangye.setTextColor(Color.BLACK);
		if (null != mApp.hPositionName.get("position_address_second")
				&& !mApp.hPositionName.get("position_address_second")
						.equals(""))
			p_address_txt.setText(mApp.hPositionName
					.get("position_address_second"));
		p_address_txt.setTextColor(Color.BLACK);
		if (null != mApp.hPositionName.get("position_positiontype")
				&& !mApp.hPositionName.get("position_positiontype").equals(""))
			p_positiontype_txt.setText(mApp.hPositionName
					.get("position_positiontype"));
		p_positiontype_txt.setTextColor(Color.BLACK);
		if (null != mApp.hPositionName.get("position_companysize")
				&& !mApp.hPositionName.get("position_companysize").equals(""))
			p_companysize_txt.setText(mApp.hPositionName
					.get("position_companysize"));
		p_companysize_txt.setTextColor(Color.BLACK);
		if (null != mApp.hPositionName.get("position_salary")
				&& !mApp.hPositionName.get("position_salary").equals(""))
			p_salary_txt.setText(mApp.hPositionName.get("position_salary"));
		p_salary_txt.setTextColor(Color.BLACK);

		p_positiontype_main.setOnClickListener(listener);
		p_companysize_main.setOnClickListener(listener);
		p_position_name.setOnClickListener(listener);
		p_type_main.setOnClickListener(listener);
		p_address_main.setOnClickListener(listener);
		p_salary_main.setOnClickListener(listener);
		if (mFullSearch) {
			title.setText("精准搜索");

			p_positiontype_main.setVisibility(View.VISIBLE);

			p_companysize_main.setVisibility(View.VISIBLE);

			p_salary_main.setVisibility(View.VISIBLE);

			p_type_main.setVisibility(View.VISIBLE);
		} else {
			p_positiontype_main.setVisibility(View.GONE);

			p_companysize_main.setVisibility(View.GONE);

			p_salary_main.setVisibility(View.GONE);

			p_type_main.setVisibility(View.GONE);
		}
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.p_position_name:// 职位名称

				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_name");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			case R.id.p_type_main:// 行业
				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_hangye");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			case R.id.p_address_main: // 工作地点
				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_address");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			case R.id.p_positiontype_main:// 职位性质
				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_positiontype");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			case R.id.p_companysize_main:// 公司规模
				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_companysize");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			case R.id.p_salary_main:// 薪水范围
				intent = new Intent(SearchJobActivity.this,
						SearchPositionList.class);
				intent.putExtra("type", "position_salary");
				intent.putExtra("full", mFullSearch);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"SearchJobActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}
